package com.cn.jdia.tas.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cn.jdia.tas.context.SpringContextUtils;
import com.cn.jdia.tas.model.T_employee;
import com.cn.jdia.tas.service.IEmployeeService;
import com.cn.jdia.tas.service.IInterestService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@Controller
@RequestMapping("/control")
public class InterestListController {

	@Resource
	public IEmployeeService service =null;
	
	@Resource
	private IInterestService interestService = null;
	
	@RequestMapping("/interestlist")
	public String getInterestList(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");
		Gson gson = new Gson();
		Map<String, Object> pmap = gson.fromJson(data, new TypeToken<Map<String, Object>>() {
		}.getType());
		
		List<String> list = (List<String>) pmap.get("list");
		System.out.println(list);
		
		
//		ClassPathXmlApplicationContext w_context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
//		service=(IEmployeeService)w_context.getBean("employeeService");
		
		service=(IEmployeeService)SpringContextUtils.getContext().getBean("employeeService");
		
		List<Map<String,String>> retList=new ArrayList<>();
		List<T_employee> employeeList = service.getInterestList(list);
		for(T_employee te:employeeList){
			System.out.println(te.getEmployeename()+" , "+te.getDepatment().getDeptname());
			Map<String,String> map=new HashMap<>();
			map.put("name",te.getEmployeename());
			map.put("dept",te.getDepatment().getDeptname());
			retList.add(map);
		}
		
		out.write(gson.toJson(retList));
		out.flush();
		out.close();
		
		return null;
	}
	
	
	@RequestMapping("/interestlistbyid")
	public String getInterestListById(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");
		Gson gson = new Gson();
		Map<String, String> pmap = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());
		String id = pmap.get("id");
		
//		ClassPathXmlApplicationContext w_context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
//		interestService = (IInterestService) w_context.getBean("interestService");
		
		interestService = (IInterestService)SpringContextUtils.getContext().getBean("interestService");
		
		List<String> interestList = interestService.getInterestList(id);	
		
		List<Map<String,String>> retList=new ArrayList<>();
		if(interestList.size()==0){
			out.write(gson.toJson(retList));
			out.flush();
			out.close();
			
			return null;
		}
		
//		service=(IEmployeeService)w_context.getBean("employeeService");
		service=(IEmployeeService)SpringContextUtils.getContext().getBean("employeeService");
		
		List<T_employee> employeeList = service.getInterestList(interestList);
		for(T_employee te:employeeList){
			//System.out.println(te.getEmployeename()+" , "+te.getDepatment().getDeptname());
			Map<String,String> map=new HashMap<>();
			map.put("employeid", te.getEmployeeid());
			map.put("name",te.getEmployeename());
			map.put("dept",te.getDepatment().getDeptname());
			retList.add(map);
		}
		
		String json = gson.toJson(retList);
		System.out.println(json);
		
		out.write(json);
		out.flush();
		out.close();
		
		return null;
	}
}
